/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {
    loadList('/v1/teacher/program/list.json');

    $('#view-apply-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        $(this).find('.program-id').val(id);
    });
    $('#view-apply-dialog').on('hide.bs.modal', function(){
        $(this).find('.program-id').val('');
    });
    $('#view-apply-dialog .btn-save').on('click', function(){
        var id = $('#view-apply-dialog .program-id').val();

        sendAjax('/v1/teacher/program/apply', 'post', {id:id}, function(json){
            $('#view-apply-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });
});
function loadList(url) {
    $("#program-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'title',
            title: '项目名称'
        }, {
            field: 'apply_time',
            title: '开放申请时间'
        }, {
            field: 'state',
            title: '项目状态',
            formatter: function (value) {
                switch (value) {
                    case 1 : return '申请中';
                    case 2 : return '选房中';
                    case 3 : return '已完成';
                    default: return '申请中';
                }
            }
        }, {
            field: 'is_apply',
            title: '申请状态'
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                var action = '<a class="btn btn-xs btn-info view-rent" href="/teacher/program/view/'+row.id+'.html">详情</a>';
                if (row.state == 1) action += '<a class="btn btn-xs btn-primary view-apply" data-toggle="modal" data-target="#view-apply-dialog" data-id="'+row.id+'">申请</a>';
                return action;
            }
        }]
    });
}

function deleteRepair(url, id, callback) {
    $.post(url, {id:id}, function(e){
        var json = JSON.parse(e);
        callback(json);
    });
}